package com.lovo.project.controller;

import com.lovo.project.bean.JurisdictionBean;
import com.lovo.project.bean.RoleBean;
import com.lovo.project.service.JurisdictionService;
import com.lovo.project.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/jurisdiction")
public class JurisdictionController {
    @Autowired
    JurisdictionService jurisdictionService;
    @Autowired
    RoleService roleService;

    @RequestMapping("/queryJurisdictionList")
    public List<JurisdictionBean> queryJurisdictionList(@RequestParam("roleId") List<Integer> roleId) {
        List<RoleBean> list = jurisdictionService.queryJurisdictionList(roleId);
        List<JurisdictionBean> listJurisdiction = new ArrayList<>();
        for (RoleBean b : list) {
            List<JurisdictionBean> jurisdictionBeans = b.getJurisdictionList();
            for (JurisdictionBean a : jurisdictionBeans) {
                listJurisdiction.add(a);
            }
        }
        return listJurisdiction;
    }

    @RequestMapping("/queryJurisdictionList1")
    public List<JurisdictionBean> queryJurisdictionList1() {
        List<JurisdictionBean> list = jurisdictionService.selectJurisdictionList1(null);
        return list;
    }

    @RequestMapping("/updateJurisdiction")
    public String updateJurisdiction(@RequestParam("roleId") Integer roleId, @RequestParam("jurisdictionId") List<Integer> jurisdictionId) {
        jurisdictionService.updateJurisdiction(roleId, jurisdictionId);
        return "OK";
    }

    @RequestMapping("/addRole")
    public String addRole(@RequestParam("roleName") String roleName, @RequestParam("jurisdictionId") List<Integer> jurisdictionId) {
        RoleBean roleBean = new RoleBean();
        roleBean.setRoleName(roleName);
        jurisdictionService.addRole(roleBean, jurisdictionId);
        return "OK";
    }

    @RequestMapping("/queryJurisdictionList2")
    public List<Integer> queryJurisdictionList2(Integer userId) {
        List<RoleBean> list1 = roleService.queryUserRoleList(userId);
        List<Integer> list2=new ArrayList<>();
        for (RoleBean b : list1){
            list2.add(b.getRoleId());
        }
        List<RoleBean> list = jurisdictionService.queryJurisdictionList(list2);
        List<Integer> listJurisdiction = new ArrayList<>();
        for (RoleBean b : list) {
            List<JurisdictionBean> jurisdictionBeans = b.getJurisdictionList();
            for (JurisdictionBean a : jurisdictionBeans) {
                listJurisdiction.add(a.getJurisdictionId());
            }
        }
        return listJurisdiction;
    }
}
